package com.ysd.lis.service.eq.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.Result;
import com.ysd.lis.entity.SysUser;
import com.ysd.lis.entity.WkFile;
import com.ysd.lis.entity.WkFolder;
import com.ysd.lis.entity.eq.EqEquipmentImage;
import com.ysd.lis.mapper.WkFileMapper;
import com.ysd.lis.mapper.WkFolderMapper;
import com.ysd.lis.mapper.eq.EqEquipmentImageMapper;
import com.ysd.lis.service.eq.EqEquipmentImageService;
import com.ysd.util.RedisUserManager;
import com.ysd.util.ToolsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 设备管理-设备类型 服务实现类
 * </p>
 *
 * @author prs
 * @since 2024-07-31
 */
@Service
public class EqEquipmentImageServiceImpl extends ServiceImpl<EqEquipmentImageMapper, EqEquipmentImage> implements EqEquipmentImageService {

    @Autowired
    private EqEquipmentImageMapper eqEquipmentImageMapper;
    private static final int MAX_POST_SIZE = 20 * 1024 * 1024;
    @Value("${wkFilePath}")
    private String filePath;

    @Autowired
    WkFileMapper wkFileMapper;
    @Autowired
    WkFolderMapper wkFolderMapper;

    @Override
    public Result uploadEqEquipmentImage(String id, Map<String, String> map) {
        int seq = 1;

        EqEquipmentImage eqEquipmentImage = new EqEquipmentImage();
        eqEquipmentImage.setEquiptId(id);
        eqEquipmentImage.setImagePath(map.get("path"));
        eqEquipmentImage.setImageName(map.get("name"));
        eqEquipmentImage.setSeq(seq++);
        this.save(eqEquipmentImage);

        return Result.succ(1, "上传成功", eqEquipmentImage);
    }

    @Override
    public Result uploadEqEquipmentDoc(MultipartFile file, HttpServletRequest request) throws IOException {

        String businessId = request.getParameter("businessId");
        String fileId = request.getParameter("fileId");
        String businessName = request.getParameter("businessName");
        String businessCode = request.getParameter("businessCode");

    /*    List<WkFolder> wkFolders = wkFolderMapper.selectList(new LambdaQueryWrapper<WkFolder>().eq(WkFolder::getPid, fileId).eq(WkFolder::getCode, businessCode).eq(WkFolder::getDelFlag, 0));
        WkFolder wkFolder1=null;
        if (CollectionUtil.isEmpty(wkFolders)){
            WkFolder wkFolder = new WkFolder();
            wkFolder.setCode(businessCode);
            wkFolder.setName(businessName+"("+businessCode+")");
            wkFolder.setPid(fileId);
            wkFolder.setGrade(0);
            wkFolderMapper.insert(wkFolder);
            wkFolder1=wkFolder;
        }else {
            wkFolder1=wkFolders.get(0);
        }
*/

        String name = file.getOriginalFilename();
        // 文件限制10M
        long size = file.getSize();
        if (size > MAX_POST_SIZE) {
            return Result.fail("length exceeds limit of 10M");
        }

        String folder = filePath + "\\" + DateUtil.format(new Date(), "yyyyMM") + File.separator;
        if (!FileUtil.exist(folder)) {
            FileUtil.mkdir(folder);
        }
        String type = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        String fileName = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddhhmmssSSS")) + type;
        String path = folder + fileName;


        WkFile wkFile = new WkFile();
        wkFile.setBusinessId(businessId);
        wkFile.setFolderId(fileId);
        wkFile.setName(name);
        wkFile.setDocPath(path);
        wkFile.setFileExten(name.substring(name.lastIndexOf(".") + 1));
        wkFile.setFileSize(String.valueOf(size));
        wkFileMapper.insert(wkFile);
        File files1 = new File(path);
        if (FileUtil.exist(files1)) {
            return Result.fail("文件已存在");
        }
        File file1 = FileUtil.writeBytes(file.getBytes(), path);
        if (file1.length() < 0) {
            return Result.fail("文件上传失败");
        }
        return Result.succ(1, "上传成功", wkFile);
    }
}
